import Vue from "vue";
import VueRouter from "vue-router";
import layout from "@/layout/index";

Vue.use(VueRouter);

const routes = [
  // 登录页面
  {
    path: "/",
    component: (resolve) => require(["@/views/login/index"], resolve),
  },
  {
    path: "/page1",
    name: "官网",
    component:resolve => require(["@/views/page1/index"],resolve)
  },
  // 首页
  {
    path: "/home",
    component: layout,
    name: "系统管理",
    children: [
      {
        path: "index",
        name: "Home",
        component: (resolve) => require(["@/views/home/home/index"], resolve),
        meta: {
          title: "首页",
        },
      },
    ],
  },
  // 仓库
  {
    path: "/warehouse",
    component: layout,
    meta: {
      title: "仓库管理",
    },
    children: [
      {
        path: "adminSystem",
        name: "AdminSystem",
        component: (resolve) =>
          require(["@/views/home/warehouse/adminSystem/index"], resolve),
        meta: {
          title: "管理员管理",
        },
      },
      {
        path: "checkSystem",
        name: "CheckSystem",
        component: (resolve) =>
          require(["@/views/home/warehouse/checkSystem/index"], resolve),
        meta: {
          title: "盘点单管理",
        },
      },
      {
        path: "commodity",
        name: "Commodity",
        component: (resolve) =>
          require(["@/views/home/warehouse/commodity/index"], resolve),
        meta: {
          title: "商品管理",
        },
      },
      {
        path: "deliverySystem",
        name: "DeliverySystem",
        component: (resolve) =>
          require(["@/views/home/warehouse/deliverySystem/index"], resolve),
        meta: {
          title: "出货单管理",
        },
      },
      {
        path: "distribution",
        name: "Distribution",
        component: (resolve) =>
          require(["@/views/home/warehouse/distribution/index"], resolve),
        meta: {
          title: "配送员管理",
        },
      },
      {
        path: "orderLog",
        name: "OrderLog",
        component: (resolve) =>
          require(["@/views/home/warehouse/orderLog/index"], resolve),
        meta: {
          title: "订单日志",
        },
      },
      {
        path: "purchase",
        name: "Purchase",
        component: (resolve) =>
          require(["@/views/home/warehouse/purchase/index"], resolve),
        meta: {
          title: "进货单管理",
        },
      },
      {
        path: "recycle",
        name: "Recycle",
        component: (resolve) =>
          require(["@/views/home/warehouse/recycle/index"], resolve),
        meta: {
          title: "回收单管理",
        },
      },
      {
        path: "warehouseDate",
        name: "WarehouseDate",
        component: (resolve) =>
          require(["@/views/home/warehouse/warehouseDate/index"], resolve),
        meta: {
          title: "仓库数据",
        },
      },
    ],
  },
  // 订单
  {
    path: "/order",
    component: layout,
    meta: {
      title: "订单管理",
    },
    children: [
      {
        path: "changeOrder",
        component: (resolve) =>
          require(["@/views/home/order/changeOrder/index"], resolve),
        meta: {
          title: "变更订单",
        },
      },
      {
        path: "userOrder",
        component: (resolve) =>
          require(["@/views/home/order/userOrder/index"], resolve),
        meta: {
          title: "用户订单",
        },
      },
      {
        path: "userReservation",
        component: (resolve) =>
          require(["@/views/home/order/userReservation/index"], resolve),
        meta: {
          title: "用户预约单",
        },
      },
      {
        path: "userReturn",
        component: (resolve) =>
          require(["@/views/home/order/userReturn/index"], resolve),
        meta: {
          title: "用户退回单",
        },
      },
    ],
  },
  // 商品
  {
    path: "/product",
    component: layout,
    meta: {
      title: "商品管理",
    },
    children: [
      {
        path: "productList",
        component: (resolve) =>
          require(["@/views/home/product/productList/index"], resolve),
        meta: {
          title: "商品列表",
        },
      },
      {
        path: "productType",
        component: (resolve) =>
          require(["@/views/home/product/productType/index"], resolve),
        meta: {
          title: "商品类型",
        },
      },
    ],
  },
  // 用户
  {
    path: "/user",
    component: layout,
    meta: {
      title: "用户管理",
    },
    children: [
      {
        path: "address",
        component: (resolve) =>
          require(["@/views/home/user/address/index"], resolve),
        meta: {
          title: "收货地址",
        },
      },
      {
        path: "commodityRecord",
        component: (resolve) =>
          require(["@/views/home/user/commodityRecord/index"], resolve),
        meta: {
          title: "商品记录",
        },
      },
      {
        path: "userCommodity",
        component: (resolve) =>
          require(["@/views/home/user/userCommodity/index"], resolve),
        meta: {
          title: "用户商品",
        },
      },
      {
        path: "userList",
        component: (resolve) =>
          require(["@/views/home/user/userList/index"], resolve),
        meta: {
          title: "用户列表",
        },
      },
    ],
  },
  // 大屏
  {
    path: "/screenGroup",
    component: layout,
    name: "screenGroup",
    meta: {
      title: "大屏展示",
    },
    children: [
      {
        path: "index",
        component: (resolve) => require(["@/views/screenGroup/index"], resolve),
        name: "index",
        meta: {
          title: "大屏展示",
        },
      },
    ],
  },
  // 404
  {
    path: "*",
    component: (resolve) => require(["@/views/errorPage"], resolve),
  },
];

const router = new VueRouter({
  // mode: "history",
  base: process.env.BASE_URL,
  routes,
});

// 全局导航守卫
router.beforeEach((to, from, next) => {
  // 获取本地缓存的用户信息
  let user = window.localStorage.getItem("user");
  // 如果本地有用户信息，允许跳转
  // 判断是否登录
  if (user !== null) {
    return next();
  } else if (to.path === "/") {
    return next();
  } else {
    // 跳转至登录页面
    return next({ path: "/" });
  }
});

export default router;
